﻿using Dapper;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using TransControlSystem.Model;

namespace TransControlSystem.Dao
{
    public class DapperTools
    {
        public static string ConnectionString = ConfigurationManager.ConnectionStrings["MyStrConn"].ConnectionString;

        /// <summary>
        /// 添加.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <param name="t">传入实体类型.</param>
        /// <returns>int.</returns>
        public static int Add<T>(string sql, T t)
            where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return connection.Execute(sql, t);
            }
        }

        /// <summary>
        /// 批量添加.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <param name="t">传入泛型类.</param>
        /// <returns>int.</returns>
        public static int Add<T>(string sql, List<T> t)
            where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return connection.Execute(sql, t);
            }
        }

        /// <summary>
        /// 删除.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <param name="t">传入实体类型.</param>
        /// <returns>int.</returns>
        public static int Delete<T>(string sql, T t)
              where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return connection.Execute(sql, t);
            }
        }

        /// <summary>
        /// 批量删除.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <param name="t">传入泛型类.</param>
        /// <returns>int.</returns>
        public static int Delete<T>(string sql, List<T> t)
              where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return connection.Execute(sql, t);
            }
        }

        /// <summary>
        /// 修改.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <param name="t">传入实体类型.</param>
        /// <returns>int.</returns>
        public static int Update<T>(string sql, T t)
              where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return connection.Execute(sql, t);
            }
        }

        /// <summary>
        /// 批量修改.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <param name="t">传入泛型类.</param>
        /// <returns>int.</returns>
        public static int Update<T>(string sql, List<T> t)
              where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return connection.Execute(sql, t);
            }
        }

        /// <summary>
        /// 查询.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <returns>泛型类.</returns>
        public static List<T> Query<T>(string sql)
             where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return connection.Query<T>(sql).ToList();
            }
        }
        /// <summary>
        /// 查询.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <returns>泛型类.</returns>
        public static ObservableCollection<T> QueryOc<T>(string sql)
             where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return new ObservableCollection<T>(connection.Query<T>(sql).ToList());
            }
        }

        /// <summary>
        /// 查询指定数据.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <param name="t">传入泛型类.</param>
        /// <returns>类.</returns>
        public static T Query<T>(string sql, T t)
             where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return connection.Query<T>(sql, t).SingleOrDefault();
            }
        }

        /// <summary>
        /// 查询的in操作.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <returns>泛型类.</returns>
        public static List<T> Query<T>(string sql, int[] ids)
            where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return connection.Query<T>(sql, new { ids }).ToList();
            }
        }

        /// <summary>
        /// 查询的in操作.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        /// <returns>泛型类.</returns>
        public static ObservableCollection<T> QueryOc<T>(string sql, int[] ids)
            where T : class
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                return new ObservableCollection<T>(connection.Query<T>(sql, new { ids }).ToList());
            }
        }

        /// <summary>
        /// 多语句操作.
        /// </summary>
        /// <typeparam name="T">实体类型.</typeparam>
        /// <param name="sql">传入sql执行语句.</param>
        public static void QueryMultiple(string sql)
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                var multiReader = connection.QueryMultiple(sql);
                var tagInfo = multiReader.Read<TagInfo>();
                multiReader.Dispose();
            }
        }

        /// <summary>
        /// 执行SQL脚本
        /// </summary>
        /// <param name="SqlScript">SQL脚本</param>
        public static void ExecuteScript(string SqlScript)
        {
            using (IDbConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Execute(SqlScript);
            }
        }
    }
}
